package xiaohong.sort;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 插入排序
 */
public class InsertSortDemo {

    public static void main(String[] args) {

        List<Integer> nums = new ArrayList<>(10);
        for (int i = 0; i < 10; i++) {
            nums.add((int)(Math.random() * 100));
        }
        System.out.println(nums);
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println( "插入排序之前时间" +formatter.format(date));
        for (int i = 1; i < nums.size(); i++) {
            int num = nums.get(i);
            int index  = i - 1;
            while (index >= 0 && num < nums.get(index)) {
                nums.set(index + 1, nums.get(index));
                index --;
            }
            nums.set(index + 1, num);
        }
        Date date1 = new Date();
        System.out.println( "插入排序之后时间" +formatter.format(date1));
        System.out.println(nums);
    }



}
